/*
 * Created by SharpDevelop.
 * User: Guy Fomi
 * Date: 2/6/2011
 * Time: 2:14 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Web.Script.Serialization;
using FileSystemDatabase;
using TomatoSynchronizer.Entities;

namespace TomatoSynchronizer.Store
{
	/// <summary>
	/// Description of AttributStore.
	/// </summary>
	public class AttributStore : IStore<ProductAttributGroup>
	{
		private Site _site;
		public Site site {
			get { return _site; }
			set { _site = value; }
		}
		
		private HtmlWeb _client;
		
		private JsonDatabase db;		
		
		public AttributStore(Site site,HtmlWeb client)
		{
			db = new JsonDatabase("sites/" + site.Name + "/Attribut");
			_site = site;
			_client = client;
		}
		
		private List<ProductAttributGroupEntry> GetAttributEntries(string groupId)
		{
			List<ProductAttributGroupEntry> entries = new List<ProductAttributGroupEntry>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_products_attributes_entries&module=products_attributes&products_attributes_groups_id=" + groupId);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					ArrayList recs = (ArrayList)json[key];
					for(int i = 0;i < recs.Count;i++)
					{
						ProductAttributGroupEntry entry = (ProductAttributGroupEntry)recs[i];
						entries.Add(entry);
					}
				}
			}
			
			return entries;
		}
		
		public void InsertItem(ProductAttributGroup item)
		{
			throw new NotImplementedException();
		}
		
		public void PersistItems(List<ProductAttributGroup> items)
		{
			db.Write<List<ProductAttributGroup>>(items,_site.Name);
		}
		
		public void Truncate()
		{
			throw new NotImplementedException();
		}
		
		public void PersistItem(ProductAttributGroup item)
		{
			db.Write<ProductAttributGroup>(item,item.products_ProductAttributGroupes_groups_id.ToString());
		}
		
		public bool MoveItem(string itemId, string folderId)
		{
			throw new NotImplementedException();
		}
		
		public bool DeleteItem(string itemId)
		{
			throw new NotImplementedException();
		}
		
		public ProductAttributGroup[] LoadItems()
		{
			ProductAttributGroup[] _attributs = db.Read<ProductAttributGroup>();
			
			foreach(ProductAttributGroup attribut in _attributs)
			{
				attribut.Store = this;
			}
			
			return _attributs;
		}
		
		public ProductAttributGroup LoadItem(string itemId)
		{
			ProductAttributGroup attribut = db.Read<ProductAttributGroup>(itemId);
			if(attribut != null)
			{
				attribut.Store = this;
			}
			
			return attribut;
		}
		
		public bool Contains(ProductAttributGroup item)
		{
			throw new NotImplementedException();
		}
		
		public List<ProductAttributGroup> GetItems()
		{
			List<ProductAttributGroup> attributs = new List<ProductAttributGroup>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_products_attributes&module=products_attributes");
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					ArrayList recs = (ArrayList)json[key];
					for(int i = 0;i < recs.Count;i++)
					{
						ProductAttributGroup attribut = (ProductAttributGroup)recs[i];
						attribut.Entries = GetAttributEntries(attribut.products_ProductAttributGroupes_groups_id.ToString());
						attributs.Add(attribut);
					}
				}
			}
			
			return attributs;
		}
		
		public ProductAttributGroup GetItem(string itemId)
		{
			throw new NotImplementedException();
		}
		
		public bool UpdateItem(ProductAttributGroup item)
		{
			throw new NotImplementedException();
		}
		
		string IStore<ProductAttributGroup>.InsertItem(ProductAttributGroup item)
		{
			throw new NotImplementedException();
		}
	}
}
